package com.dems.web;

import com.dems.dao.RepairDao;
import com.dems.dao.StudentDao;
import com.dems.domain.Repair;
import com.dems.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/MpUpdateRepairServlet")
public class MpUpdateRepairServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private RepairDao repairDao = new RepairDao();
    private StudentDao studentDao = new StudentDao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MpUpdateRepairServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String repairType = request.getParameter("repairType");
        String repairDescription = request.getParameter("repairDescription");
        String repairState = request.getParameter("repairState");
        String studentNum = request.getParameter("studentNum");
        //根据账号查询学生信息
        Student student = studentDao.selectOne(studentNum);
        String buildingId = student.getBuildingId();
        String studentName = student.getStudentName();
        String studentPhone = student.getStudentPhone();
        String studentDormitory = student.getStudentDormitory();

        Repair repair = new Repair(0,repairType,repairDescription,repairState,buildingId,studentNum
                ,studentName,studentPhone,studentDormitory);
        try {
            repairDao.saveRepair(repair);
        } catch (Exception e) {
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
            return;
        }
        //跳转回首页
        response.getWriter().write(
                "<h1 align='center'><font color='red'>" +"恭喜注册成功，3秒之后跳转回首页</font></h1>");
        response.setHeader("refresh","3;url="+request.getContextPath()+"/index.jsp");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}


